package com.pi.http.base.configure;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.pi.http.base.filter.CommParamFilter;
import com.pi.http.base.interceptor.AuthInterceptor;

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport{
  
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    InterceptorRegistration addedInterceptor = registry.addInterceptor(new AuthInterceptor());
 // 排除配置
    addedInterceptor.excludePathPatterns("/weChatBind**");
    
    // 拦截配置
    addedInterceptor.addPathPatterns("/**");
    super.addInterceptors(registry);
  }
  
//  @Bean
//  public AuthInterceptor getAuthInterceptor(){
//    return new AuthInterceptor();
//  }
  
  @Bean
  public FilterRegistrationBean registerFilter(){
    FilterRegistrationBean filterRegister = new FilterRegistrationBean();
    filterRegister.setFilter(new CommParamFilter());
    filterRegister.addUrlPatterns("/*");
    filterRegister.setOrder(1);
    return filterRegister;
  }
  
  @Override
  protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    ObjectMapper objectMapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addSerializer(new ToStringSerializer(Long.TYPE));
    module.addSerializer(new ToStringSerializer(Long.class));
    module.addSerializer(new ToStringSerializer(BigInteger.class));
    objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
    @Override
    public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
      jsonGenerator.writeString(null != o ? String.valueOf(o) : null);
    }
    });
    objectMapper.registerModule(module);
    converter.setObjectMapper(objectMapper);

    //这里是fastJSON的配置方式，更多的内容可以查看SerializerFeature
//    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//    converter.setFeatures(SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero,
//    SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullListAsEmpty);
//    FastJsonConfig fastJsonConfig = new FastJsonConfig();
//    SerializeConfig serializeConfig = SerializeConfig.globalInstance;
//    serializeConfig.put(Long.class, com.alibaba.fastjson.serializer.ToStringSerializer.instance);
//    serializeConfig.put(Long.TYPE, com.alibaba.fastjson.serializer.ToStringSerializer.instance)
//    fastJsonConfig.setSerializeConfig(serializeConfig);
//    converter.setFastJsonConfig(fastJsonConfig);
    converters.add(converter);
  }
}
